package com.example.exercise.app.feedback.controller;

import com.example.exercise.core.annotations.NotAutoLog;
import com.example.exercise.core.base.dto.BaseResponse;
import com.example.exercise.app.feedback.dto.request.MgFeedbackRequest;
import com.example.exercise.app.feedback.dto.response.FeedbackDetail;
import com.example.exercise.app.feedback.service.AppFeedbackService;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("mg/feedback")
//@Api(description = "用户反馈 -- 回复、忽略、查询列表及详情")
public class MgFeedbackController {

    @Resource
    private AppFeedbackService feedbackService;

//    @ApiOperation(value = "用户反馈--回复或忽略操作", notes = "replyType-1：回复,replyType-2：忽略")
    @RequestMapping(value = "saveReplyInfo", method = { RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE })
    public BaseResponse<Object> saveReplyInfo(int replyType, int id, String context, String userId) {
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        boolean[] boolArr = feedbackService.canFeedback(id);
        if(replyType == 1 && !boolArr[0]){
            baseResponse.setCode(1);
            baseResponse.setMsg("回复次数太多");
            return baseResponse;
        }
        if(replyType == 2 && !boolArr[1]){
            baseResponse.setCode(2);
            baseResponse.setMsg("已经忽略此反馈");
            return baseResponse;
        }
        if(replyType == 1 || replyType == 2){
            feedbackService.saveFeedbackReplyInfo(replyType,id,context,userId);
            baseResponse.ok();
        }
        return baseResponse;
    }

    @NotAutoLog
//    @ApiOperation(value = "用户反馈列表查询", notes = "用户反馈列表查询，fieldNum:1--用用户名查询，2--用手机号查询")
    @RequestMapping(value = "getFeedbackList", method = { RequestMethod.GET}, produces = {MediaType.APPLICATION_JSON_VALUE })
    public BaseResponse<Map<String, Object>> getFeedbackList(MgFeedbackRequest feedbackRequest) {
        BaseResponse<Map<String, Object>> baseResponse = new BaseResponse<>();
        Object[] object = feedbackService.getFeedbackList(feedbackRequest);
        Map<String, Object> map = new HashMap<>();
        map.put("count",object[0]);
        map.put("list",object[1]);
        baseResponse.ok(map);
        return baseResponse;
    }

//    @ApiOperation(value = "用户反馈详情查询", notes = "用户反馈详情查询")
    @RequestMapping(value = "getDetail", method = { RequestMethod.GET}, produces = {MediaType.APPLICATION_JSON_VALUE })
    public BaseResponse<FeedbackDetail> getFeedbackDetail(Integer id) {
        BaseResponse<FeedbackDetail> baseResponse = new BaseResponse<>();
        FeedbackDetail feedbackDetail = feedbackService.getDetail(id);
        if(feedbackDetail == null){
            baseResponse.setCode(-12);
            baseResponse.setMsg("数据为空");
        }else{
            baseResponse.ok(feedbackDetail);
        }
        return baseResponse;
    }
}
